package controller

import com.alibaba.fastjson.JSONObject
import com.huawei.services.runtime.Context
import com.huawei.services.runtime.entity.apig.APIGTriggerEvent
import com.huawei.services.runtime.entity.apig.APIGTriggerResponse
import controller.vo.JsonBody
import init.InitParams
import org.hibernate.Session
import org.hibernate.SessionFactory
import org.hibernate.boot.MetadataSources
import org.hibernate.boot.registry.StandardServiceRegistry
import org.hibernate.boot.registry.StandardServiceRegistryBuilder
import java.lang.Exception

class MainController {
    private val userController by lazy { UserController() }
    private val activityController by lazy { ActivityController() }

    fun init(context: Context?) {
        InitParams.session // init session by `lazy`
    }


    fun request(event: APIGTriggerEvent?, context: Context?): APIGTriggerResponse? {
        if (event?.httpMethod?.toUpperCase().equals("OPTIONS")) {
            val requestHeader = event?.headers
            val headers = HashMap<String, String>()
            headers["Access-Control-Allow-Origin"] = requestHeader?.get("origin") ?: "*"
            headers["Access-Control-Allow-Headers"] = requestHeader?.get("access-control-request-headers")
                    ?: "Origin, Content-Type, Cookie,X-CSRF-TOKEN, Accept,Authorization,function-code"
            headers["Access-Control-Allow-Methods"] = "GET, POST, PATCH, PUT, OPTIONS, DELETE"
            return APIGTriggerResponse(
                    204, headers, null
            )
        }
        try {
            println(InitParams.session)
            val headers: MutableMap<String, String> = event!!.headers
            println("====headers====\n $headers")
            val functionCode = Integer.parseInt(headers["function-code"])
            val pathParameters = event.pathParameters
            println("====pathParameters====\n $pathParameters")
            val queryStringParameters = event.queryStringParameters
            println("====queryStringParameters====\n $queryStringParameters")
            val body = event.rawBody
            println("====rawBody====\n $body")
            val response = when (functionCode) {
                1 -> {
                    userController.getAvatar(event, context)
                }
                2 -> {
                    userController.register(event, context)
                }
                3 -> {
                    userController.getUser(event, context)
                }
                4 -> {
                    userController.changeUser(event, context)
                }
                5 -> {
                    userController.getJoinedActivities(event, context)
                }
                6 -> {
                    userController.getPublishedActivities(event, context)
                }
                7 -> {
                    userController.uploadAvatar(event, context)
                }
                8 -> {
                    userController.login(event, context)
                }
                9 -> {
                    userController.getMessages(event, context)
                }
                10 -> {
                    userController.passJoiners(event, context)
                }
                11 -> {
                    userController.signIn(event, context)
                }
                12 -> {
                    userController.getSignActivities(event, context)
                }
                13 -> {
                    userController.bindAccount(event, context)
                }
                101 -> {
                    activityController.getActivity(event, context)
                }
                102 -> {
                    activityController.getJoiners(event, context)
                }
                103 -> {
                    activityController.addApply(event, context)
                }
                104 -> {
                    activityController.getActivitiesByOrder(event, context)
                }
                105 -> {
                    activityController.getActivitiesByName(event, context)
                }
                106 -> {
                    activityController.addNewActivity(event, context)
                }
                107 -> {
                    activityController.changeActivityInfo(event, context)
                }
                108 -> {
                    activityController.getSignJoiners(event, context)
                }
                109 -> {
                    activityController.uploadBanner(event, context)
                }
                110 -> {
                    activityController.getBanner(event, context)
                }
                else -> {
                    APIGTriggerResponse(
                            403,
                            HashMap<String, String>(),
                            JSONObject.toJSONString(
                                    JsonBody(
                                            -100,
                                            "can not find function(functionCode = $functionCode)",
                                            null
                                    )
                            )
                    )
                }
            }
            response?.apply {
                addHeader("Access-Control-Allow-Origin", "*")
                addHeader("Access-Control-Allow-Headers", "Origin, Content-Type, Cookie, Accept,Authorization,function-code")
                addHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, OPTIONS, DELETE")
            }
            return response
        } catch (e: Exception) {
            e.printStackTrace()
            return APIGTriggerResponse(
                    403, HashMap<String, String>(), JSONObject.toJSONString(
                    JsonBody(
                            -100, e.message
                            ?: "occur exception", null
                    )
            )
            )
        }
    }
}